package com.laxia.aspect;

import com.laxia.config.SpringContextHolder;
import com.laxia.exception.ensure.Ensure;
import com.laxia.model.UserSession;
import com.laxia.util.RedisUtil;
import com.laxia.util.SessionUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 登录校验
 *
 * @author weishuai
 */
@Slf4j
@Aspect
@Component
@Order(1)
public class LoginValidateAspect {

    private static RedisUtil redisUtil;

    static {
        redisUtil = SpringContextHolder.getBean(RedisUtil.class);
    }


    @Before("@annotation(loginValidate)")
    public void process(JoinPoint joinPoint, LoginValidate loginValidate) {
        if (log.isDebugEnabled()) {
            String methodName = joinPoint.getSignature().getName();
            log.debug("need login method:{}", methodName);
        }

        UserSession currentUser = SessionUtil.getCurrentUser();
        Ensure.that(currentUser.isLogin()).isTrue("401");
        Ensure.that(currentUser.getStatus() == 0).isTrue("402");


    }
}
